#include "toolview.h"
#include <QGraphicsPixmapItem>

ToolView::ToolView(QWidget *parent, bool block) :
    QGraphicsView(parent),
    m_block(block),
    m_itemType(NULL_ITEM)
{
    m_Scene = new QGraphicsScene(this);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setScene(m_Scene);
    m_Scene->setSceneRect(0.0, 0.0, 240.0, 60.0);

    m_ShadowItem = m_Scene->addPixmap(QPixmap(":/image/menu_p.png", "png"));
    m_ShadowItem->setZValue(2);
    m_ShadowItem->setVisible(false);

    if(m_block)
        m_lockItem = m_Scene->addPixmap(QPixmap(":/image/Tools (1).png","png"));
    else
        m_lockItem = m_Scene->addPixmap(QPixmap(":/image/Tools (2).png","png"));
    m_lockItem->setPos(0,0);
    m_lockItem->setZValue(1);

    m_topItem = m_Scene->addPixmap(QPixmap(":/image/Tools (3).png","png"));
    m_topItem->setPos(60,0);
    m_topItem->setZValue(1);

    m_buttomItem = m_Scene->addPixmap(QPixmap(":/image/Tools (4).png","png"));
    m_buttomItem->setPos(120,0);
    m_buttomItem->setZValue(1);

    m_deleteItem = m_Scene->addPixmap(QPixmap(":/image/Tools (5).png","png"));
    m_deleteItem->setPos(180,0);
    m_deleteItem->setZValue(1);
}

ToolView::~ToolView()
{
    if(m_Scene != NULL)
    {
        delete m_Scene;
        m_Scene = NULL;
    }
}

void ToolView::mousePressEvent(QMouseEvent *event)
{
    if(m_lockItem->isUnderMouse())
    {
        m_itemType = LOCK_ITEM;
        m_ShadowItem->setPos(m_lockItem->pos());
        m_ShadowItem->setVisible(true);
    }
    else if(m_topItem->isUnderMouse())
    {
        m_itemType = TOP_ITEM;
        m_ShadowItem->setPos(m_topItem->pos());
        m_ShadowItem->setVisible(true);
    }
    else if(m_buttomItem->isUnderMouse())
    {
        m_itemType = BUTTOM_ITEM;
        m_ShadowItem->setPos(m_buttomItem->pos());
        m_ShadowItem->setVisible(true);
    }
    else if(m_deleteItem->isUnderMouse())
    {
        m_itemType = DELETE_ITEM;
        m_ShadowItem->setPos(m_deleteItem->pos());
        m_ShadowItem->setVisible(true);
    }
}

void ToolView::mouseReleaseEvent(QMouseEvent *event)
{
    if(m_ShadowItem->isVisible())
    {
        m_ShadowItem->setVisible(false);
    }
    switch(m_itemType)
    {
    case LOCK_ITEM:
        {
            if(m_lockItem->isUnderMouse())
            {
                m_block = !m_block;
                if(m_block)
                    m_lockItem->setPixmap(QPixmap(":/image/Tools (1).png","png"));
                else
                    m_lockItem->setPixmap(QPixmap(":/image/Tools (2).png","png"));
                emit lockitem();
            }
        }
        break;
    case TOP_ITEM:
        {
            if(m_topItem->isUnderMouse())
            {
                emit topitem();
            }
        }
        break;
    case BUTTOM_ITEM:
        {
            if(m_buttomItem->isUnderMouse())
            {
                emit buttomitem();
            }
        }
        break;
    case DELETE_ITEM:
        {
            if(m_deleteItem->isUnderMouse())
            {
                emit deleteitem();
            }
        }
        break;
    default:
        break;
    }
    m_itemType = NULL_ITEM;
}

void ToolView::setLock(bool block)
{
    m_block = block;
    if(m_block)
        m_lockItem->setPixmap(QPixmap(":/image/Tools (1).png","png"));
    else
        m_lockItem->setPixmap(QPixmap(":/image/Tools (2).png","png"));
}
